package com.test.demo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

import com.hcnetsdk.jna.HCNetSDKJNAInstance;
import com.hikvision.netsdk.HCNetSDK;
import com.hikvision.netsdk.NET_DVR_PREVIEWINFO;

/**
 * Created by jy on 2018/5/7.
 */

public class HaiKangPlayActivity extends Activity {
    private final String TAG = "HaiKangPlayActivity";

    private int m_iPlayID = -1; // return by NET_DVR_RealPlay_V30
    private SurfaceView m_osurfaceView = null;
    private int m_iStartChan;
    private int m_iLogID;
    SurfaceCallBack mSurfaceCallBack;
    private SurfaceHolder holder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_haikang_play);

        m_osurfaceView = (SurfaceView) findViewById(R.id.Sur_Player);

        holder = m_osurfaceView.getHolder();

        mSurfaceCallBack = new SurfaceCallBack();
        holder.addCallback(mSurfaceCallBack);


        m_iStartChan = getIntent().getIntExtra("m_iStartChan",-1);
        m_iLogID = getIntent().getIntExtra("m_iLogID",-1);
    }

    class SurfaceCallBack implements SurfaceHolder.Callback {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            startSinglePreview();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
        }
    }

    private void startSinglePreview() {

        NET_DVR_PREVIEWINFO previewInfo = new NET_DVR_PREVIEWINFO();
        previewInfo.lChannel = m_iStartChan;
        previewInfo.dwStreamType = 0; // substream
        previewInfo.bBlocked = 1;
        previewInfo.hHwnd = m_osurfaceView.getHolder();

        m_iPlayID = HCNetSDK.getInstance().NET_DVR_RealPlay_V40(m_iLogID,
                previewInfo, null);
        if (m_iPlayID < 0) {
            Log.e(TAG, "NET_DVR_RealPlay is failed!Err:"
                    + HCNetSDK.getInstance().NET_DVR_GetLastError());
            return;
        }

        boolean bRet = HCNetSDKJNAInstance.getInstance().NET_DVR_OpenSound(m_iPlayID);
        if (bRet) {
            Log.e(TAG, "NET_DVR_OpenSound Succ!");
        }

    }

    /**
     * @fn stopSinglePreview
     * @author zhuzhenlei
     * @brief stop preview
     * @return NULL
     */
    private void stopSinglePreview() {
        if (m_iPlayID < 0) {
            Log.e(TAG, "m_iPlayID < 0");
            return;
        }

        if (HCNetSDKJNAInstance.getInstance().NET_DVR_CloseSound()) {
            Log.e(TAG, "NET_DVR_CloseSound Succ!");
        }

        // net sdk stop preview
        if (!HCNetSDK.getInstance().NET_DVR_StopRealPlay(m_iPlayID)) {
            Log.e(TAG, "StopRealPlay is failed!Err:"
                    + HCNetSDK.getInstance().NET_DVR_GetLastError());
            return;
        }
        Log.i(TAG, "NET_DVR_StopRealPlay succ");
        m_iPlayID = -1;
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        stopSinglePreview();
    }
}
